import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/fonts/iconfont.css'
import './assets/css/global.css'
// 引入和配置axios
import axios from 'axios'
// 引入第三方的包，用来展示树形结构的数据
import ZkTable from 'vue-table-with-tree-grid'
// 引入第三方的包，用来展示富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

// 导入 NProgress 进度包
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(ZkTable)
Vue.use(VueQuillEditor)

// 使用 axios 请求拦截
axios.interceptors.request.use(config => {
  NProgress.start()
  // 为请求头对象，添加token验证的Authorization
  config.headers.Authorization = window.sessionStorage.getItem('token')
  return config
})
axios.interceptors.response.use(config => {
  NProgress.done()
  return config
})

axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
Vue.prototype.$http = axios

Vue.config.productionTip = false

Vue.filter('dateFormat', (valStr) => {
  // 1.0 使用Date构造函数初始化成时间对象
  let date = new Date(valStr)
  // 2.0 调用时间对象的方法得到对应的时间
  let y = date.getFullYear()
  let m = (date.getMonth() + 1 + '').padStart(2, '0')
  let d = (date.getDate() + '').padStart(2, '0')

  let hh = (date.getHours() + '').padStart(2, '0')
  let mm = (date.getMinutes() + '').padStart(2, '0')
  let ss = (date.getSeconds() + '').padStart(2, '0')

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

new Vue({
  data: {
    path: ''
  },
  router,
  render: h => h(App),
  mounted() {
    window.addEventListener('popstate', e => {
      window.sessionStorage.setItem('activePath', this.path)
      window.location.reload()
    }, false)
  },
  watch: {
    $route(to, from) {
      this.path = to.path
    }
  }
}).$mount('#app')
